package util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;


public class Configure {
	/** 属性文件路径 */
	private final static String FILE_NAME = "config.properties";
	/** 属性集 */
	private static Properties props = new Properties();
	/** 外放对象 */
	private static Configure propertys;

	/** 构造私有 */
	private Configure() {
		InputStream in = this.getClass().getClassLoader().getResourceAsStream(FILE_NAME);
		try {
			props.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** 公共方法获取实例 */
	public static Configure getInstans() {
		synchronized (props) {
			if (propertys == null) {
				propertys = new Configure();
			}
		}
		return propertys;
	}
	
	/**
	 * 获取微信公众号的appid
	 * @return
	 */
	public static String getAppid() {
		return getInstans().getValue("appid");
	}
	
	/**
	 * 获取微信公众号的appsecret
	 * @return
	 */
	public static String getAppsecret() {
		return getInstans().getValue("appsecret");
	}

	/**
	 * 获取微信公众号的token
	 * @return
	 */
	public static String getToken() {
		return getInstans().getValue("token");
	}

	/**
	 * 获取微信公众号的encodingAESKey
	 * @return
	 */
	public static String getEncodingAESKey() {
		return getInstans().getValue("encodingAESKey");
	}

	/**
	 * 获取微信公众号的id
	 * @return
	 */
	public static String getWechatid() {
		return getInstans().getValue("wechatid");
	}


	/**
	 * 获取字符串对应值
	 * 
	 * @throws IOException
	 */
	public String getValue(String key) {
		// 取出相关值
		try {
			return props.getProperty(key);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	public Map<String, String> getAllProps() {
		Map<String, String> allProp = new HashMap<String, String>();
		Set<String> propSet = props.stringPropertyNames();
		Iterator<String> iprop = propSet.iterator();// 先迭代出来
		while (iprop.hasNext()) {// 遍历
			String key = iprop.next();
			allProp.put(key, getValue(key));
		}
		return allProp;
	}

	public Iterator<String> getAllPropNames() {
		Set<String> propSet = props.stringPropertyNames();
		Iterator<String> iprop = propSet.iterator();// 先迭代出来
		return iprop;
	}
}
